//路由抽取
import Vue from 'vue'
import VueRouter from 'vue-router'
import login from '@/views/login/login.vue'
import layout from '@/views/layout/layout.vue'
import chart from '@/views/layout/chart/chart.vue'
import user from '@/views/layout/user/user.vue'
import question from '@/views/layout/question/question.vue'
import enterprise from '@/views/layout/enterprise/enterprise.vue'
import subject from '@/views/layout/subject/subject.vue'

//导入进度条 与CSS
import NProgress from 'nprogress'
import 'nprogress/nprogress.css'
//导入vuex实例对象
import store from '@/store/index.js'
import { Message } from 'element-ui'
import { removeLocal } from '@/utils/local.js'

Vue.use(VueRouter)
const router = new VueRouter({
  routes: [
    {
      path: '/',
      redirect: '/login'
    },
    {
      path: '/login',
      component: login,
      meta: {
        title: '登录页',
        roles: ['超级管理员', '管理员', '老师', '学生']
      }
    },
    {
      path: '/layout',
      component: layout,
      redirect: '/layout/subject',
      children: [
        {
          path: '/layout/chart',
          component: chart,
          meta: {
            title: '数据概览',
            roles: ['超级管理员', '管理员', '老师'],
            icon: 'el-icon-pie-chart'
          }
        },
        {
          path: '/layout/user',
          component: user,
          meta: {
            title: '用户列表',
            roles: ['超级管理员', '管理员'],
            icon: 'el-icon-user'
          }
        },
        {
          path: '/layout/question',
          component: question,
          meta: {
            title: '题库列表',
            roles: ['超级管理员', '管理员', '老师'],
            icon: 'el-icon-edit-outline'
          }
        },
        {
          path: '/layout/enterprise',
          component: enterprise,
          meta: {
            title: '企业列表',
            roles: ['超级管理员', '管理员', '老师'],
            icon: 'el-icon-office-building'
          }
        },
        {
          path: '/layout/subject',
          component: subject,
          meta: {
            title: '学科列表',
            roles: ['超级管理员', '管理员', '老师', '学生'],
            icon: 'el-icon-notebook-2'
          }
        }
      ]
    }
  ]
})

router.beforeEach((to, from, next) => {
  //进度条开始
  NProgress.start()
  if (to.meta.roles.includes(store.state.role)) {
    next()
  } else {
    Message.error('该账号没有权限')
    removeLocal('token')
    next('/login')
  }

  console.log('to', to)
  console.log('from', from)
})
router.afterEach(to => {
  //进度条结束
  NProgress.done()
  // console.log('to', to)
  // document.getElementById('title').innerText = to.meta.title
  //简写模式
  document.title = to.meta.title
})
export default router
